{*
  Object Pascal simple geometric routines
  @Author Maciej Zagozda (1997-2008)
  @Author Surinder Singh (2006-2007)
  @Author Pawel Sakowski (1997-1998)
  @Licence Microsoft Reciprocal License (Ms-RL)
  @Website http://www.codeplex.com/intelitechdelphirtns/
  @Version 2008/04/20 Added documentation support
}

unit IntegerRoutines;

interface

Uses
  Classes,
  Dialogs,
  Messages,
  SysUtils,
  Windows;

function BooleanToInteger( boolValue: Boolean ): ShortInt;
function IntegerToBoolean( intgNumber: Integer ): Boolean;
function IntegerToPercentFloat( intgNumber: Integer ): Single;
function FloatToInteger( doblFloat: Double ): Longint;
function ForceNonNegative( intgNumber: Longint ): Longint;
function ForceNonPositive( intgNumber: Longint ): Longint;
function ForcePositive( intgNumber: Longint ): Longint;
function HexStrToInt(AStr: String): Integer;
function GetCorespondingIndex( intgIndex, intgPosition, intgMin, intgMax: Integer ): Integer;
function MathRound( doblNumber: Double ): Longint;

procedure Incr( var intgNumber: Integer );
procedure Decr( var intgNumber: Integer );
procedure Zero( var intgNumber: Integer );

implementation

function BooleanToInteger( boolValue: Boolean ): ShortInt;
begin
  if boolValue then Result := 1 else Result := 0;
end;

//_____________________________________________________________________________

function IntegerToBoolean( intgNumber: Integer ): Boolean;
begin
  Result := intgNumber = 1;
end;

//_____________________________________________________________________________

function IntegerToPercentFloat( intgNumber: Integer ): Single;
begin
  Result := ( intgNumber / 100 );
end;

//_____________________________________________________________________________

function FloatToInteger( doblFloat: Double ): Longint;
begin
  Result := Trunc( doblFloat ) + Trunc ( Frac( doblFloat ) * 2 );
end;

//_____________________________________________________________________________

function ForceNonNegative( intgNumber: Longint ): Longint;
begin
  if intgNumber > 0 then
  begin
    Result := intgNumber
  end
  else
  begin
    Result := 0;
  end;//if
end;

//_____________________________________________________________________________

function ForceNonPositive( intgNumber: Longint ): Longint;
begin
  if intgNumber < 0 then
  begin
    Result := intgNumber
  end
  else
  begin
    Result := 0;
  end;//if
end;

//_____________________________________________________________________________

function ForcePositive( intgNumber: Longint ): Longint;
begin
  if intgNumber > 0 then
  begin
    Result := intgNumber;
  end
  else
  begin
    Result := 1;
  end;//if
end;

//_____________________________________________________________________________

function GetCorespondingIndex( intgIndex, intgPosition, intgMin, intgMax: Integer ): Integer;
begin

  if intgPosition > intgIndex then
  begin
    Result := intgPosition - 2;
  end
  else
  begin
    Result := intgPosition + 2;
  end;//if

  if ( Result < intgMin ) or ( Result > intgMax ) then
  begin
    Result := intgPosition;
  end;//if

end;

//_____________________________________________________________________________

function MathRound( doblNumber: Double ): Longint;
begin
  Result := Round( doblNumber - 0.49 );
end;

//_____________________________________________________________________________

function HexStrToInt(AStr: String): Integer;
var
  i: Integer;
begin
  Result := 0;
  AStr := UpperCase(AStr);
  for i := 1 to Length(AStr) do
  begin
    Result := Result * 16;
    if AStr[i] > '9' then
      Result := Result + Ord(AStr[i]) - Ord('A') + 10
    else
      Result := Result + Ord(AStr[i]) - Ord('0');
  end;
end;

//_____________________________________________________________________________

procedure Incr( var intgNumber: Integer );
begin
  intgNumber := intgNumber + 1;
end;

//_____________________________________________________________________________

procedure Decr( var intgNumber: Integer );
begin
  intgNumber := intgNumber - 1;
end;

//_____________________________________________________________________________

procedure Zero( var intgNumber: Integer );
begin
  intgNumber := 0;
end;
end.
